package com.bdqn.blog.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bdqn.blog.entity.Consumer;
import com.bdqn.blog.service.impl.ConsumerServiceImpl;
import com.bdqn.blog.util.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端用户 前端控制器
 * </p>
 *
 * @author 阿桐幕
 * @since 2020-10-29
 */
@RestController
@RequestMapping("/blog/consumer")
public class ConsumerController {

    @Autowired
    ConsumerServiceImpl consumerService;


    @GetMapping("/findConsumerAllOrByName")
    public Object findConsumerAllOrByName(String query , Integer pageIndex , Integer pageSize){
        //封装pagehelper的分页对象  参数 页面 页大小
        PageHelper.startPage(pageIndex,pageSize);
        List<Consumer> singerList = consumerService.findAllConsumerOrByName(query);
        //将集合存入到pageinfo对象中
        PageInfo<Consumer> pageInfo = new PageInfo<Consumer>(singerList);
        return Result.success(pageInfo);
    }
    //添加用户
    @PostMapping("/addConsumer")
    public Result addSinger(@RequestBody Consumer consumer){
        consumer.setAvator("/avatorImages/IMG_0776.jpg");
        boolean save = consumerService.save(consumer);
        if (save){
            return Result.success();
        }else {
            return Result.fail("添加失败");
        }
    }

    @PostMapping("/getAll")
    public Result getAll(){
        return Result.success(consumerService.list());
    }

    /**
     * 更新用户图片
     */
    @PostMapping("/updateConsumerAvator")
    public Object updateConsumerAvator(@RequestParam("file") MultipartFile avatorFile, @RequestParam("id") int id){
        if(avatorFile.isEmpty()){
            return Result.fail( "文件上传失败");
        }
        //文件名=当前时间到毫秒+原来的文件名
        String fileName = System.currentTimeMillis()+avatorFile.getOriginalFilename();
        //文件路径F:\idea-project\music\src\main\resources\static\img\singerPic
        //文件路径
        String filePath = System.getProperty("user.dir")+System.getProperty("file.separator")+"avatorImages";
        //如果文件路径不存在，新增该路径
        File file1 = new File(filePath);
        if(!file1.exists()){
            file1.mkdir();
        }
        //实际的文件地址
        File dest = new File(filePath+System.getProperty("file.separator")+fileName);
        //存储到数据库里的相对文件地址
        String storeAvatorPath = "/avatorImages/"+fileName;
        try {
            avatorFile.transferTo(dest);
            Consumer consumer = new Consumer();
            consumer.setId(id);
            consumer.setAvator(storeAvatorPath);
            boolean flag = consumerService.saveOrUpdate(consumer);
            if(flag){
                return Result.success(consumer);
            }
            return Result.fail( "文件上传失败");
        } catch (IOException e) {

            return Result.fail( "文件上传失败")+e.getMessage();
        }
    }
    //根据id查询
    @PostMapping("/getConsumerByid/{id}")
    public Result getConsumerByid(@PathVariable Integer id){
        return Result.success(consumerService.getById(id));
    }
     //删除
    @PostMapping("/delConsumerByid/{id}")
    public Result delSingerByd(@PathVariable Integer id){
        return Result.success(consumerService.removeById(id));
    }

     //修改
    @PostMapping("/SaveConsumer")
    public Result SaveSinger(@RequestBody Consumer consumer){
        boolean save = consumerService.saveOrUpdate(consumer);
        if (save){
            return Result.success();
        }else {
            return Result.fail("修改失败");
        }
    }

    /**
     *  登录验证
     * **/
    @PostMapping("/login")
    public Result login(@RequestBody Consumer consumer){
        QueryWrapper<Consumer> wrapper = new QueryWrapper<>();
        wrapper
                .eq("username",consumer.getUsername())
                .eq("password",consumer.getPassword());
        Consumer consumer1 = consumerService.getOne(wrapper);
        if (consumer1 != null ){
           return  Result.success(consumer1);
        }else {
           return Result.fail("用户名或者密码不正确！！1") ;
        }
    }


}

